package com.motion.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motion.dto.PswDto;
import com.motion.dto.UserDataDto;
import com.motion.generator.entity.User;
import com.motion.generator.entity.UserData;
import com.motion.generator.mapper.UserDataMapper;
import com.motion.generator.mapper.UserMapper;
import com.motion.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static org.apache.logging.log4j.util.Strings.isBlank;

@Service
public class UserService {
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired(required = false)
    UserDataMapper userDataMapper;

    public String test(){
        return "测试成功";
    }

//    public UserDataDto userDataDto(long id){
//        UserDataDto userDataDto=null;
//        User user=userMapper.selectById(id);
//        String name=user.getNickName();
//        userDataDto.setNickname(name);
//
//        QueryWrapper<UserData> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("UserID",id);
//        int fansnum = userDataMapper.selectOne(queryWrapper).getFansNumber();
//        userDataDto.setFansnum(fansnum);
//
//        int focusnum = userDataMapper.selectOne(queryWrapper).getFocusNumber();
//        userDataDto.setFansnum(focusnum);
//
//        String headPic = user.getHeadPortraitUrl();
//        userDataDto.setHeadPic(headPic);
//        return userDataDto;
//    }

    public void createUser(User user) {
        userMapper.insert(user);
    }

    public boolean isExitUser(String mobile) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("phoneNumber", mobile);
        List<User> lst = userMapper.selectList(wrapper);
        return lst.size() > 0;
    }

    public Long getUserId(String phoneNumber){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phoneNumber", phoneNumber);
        return userMapper.selectOne(wrapper).getUserId();
    }
    //mima
    public String getUserPsw(Long userId){
        return userMapper.selectById(userId).getPassword();
    }

    public boolean modifyPsw(Long userId, String newPsw, String oldPsw) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("userId", userId);
        User user = userMapper.selectOne(wrapper);
        if(!isBlank((user.getPassword()))){
            if (user.getPassword().equals(oldPsw)) {
                user.setPassword(newPsw);
                userMapper.updateById(user);
                return true;
            }
            else {
                return false;
            }
        } else {
            user.setPassword(newPsw);
            userMapper.updateById(user);
            return true;
        }
    }

    public  User updateUser(User user){
//        userMapper.update(user, new UpdateWrapper<User>().eq("phoneNumber", user.getPhoneNumber()));
        try{
            userMapper.updateById(user);
            return user;
        }catch(Exception e){
            return user;
        }
    }

    public User getUserById(Long userId){
        return userMapper.selectById(userId);
    }

    public IPage<User> getUserByKeyword(String keyword, Long currentPage, Long pageSize){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("phoneNumber", keyword).or().like("nickname", keyword);
        return userMapper.selectPage(new Page<>(currentPage, pageSize), queryWrapper);
    }

    public LocalDateTime getTime(String phone){
//        userMapper.update(user, new UpdateWrapper<User>().eq("phoneNumber", user.getPhoneNumber()));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phoneNumber", phone);
        LocalDateTime time=userMapper.selectOne(queryWrapper).getCreateTime();
        return time;
    }
    //用户头像上传修改
    public void setheadurl(Long useId, String url){
        User user=userMapper.selectById(useId);
        user.setHeadPortrait(url);
        userMapper.updateById(user);
    }

    public IPage<User> getUser(Long page,Long size){
        return userMapper.selectPage(new Page<>(page, size), new QueryWrapper<>());
    }
    public IPage<User> getUserByStr(Long page,Long size,int ifban,LocalDateTime starTime, LocalDateTime endTime, String searchStr){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if (ifban!=2)
            queryWrapper.eq("type",ifban);
        if(starTime!=null)
            queryWrapper.ge("createTime",starTime);
        if (endTime!=null)
            queryWrapper.le("createTime",endTime);
        if (!searchStr.equals(""))
            queryWrapper.like("nickName",searchStr).or().like("phoneNumber",searchStr);
        return userMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    public Boolean updata2UserToken(Long userId,String token){
        try {
            User user = userMapper.selectById(userId);
            user.setToken(token);
            userMapper.updateById(user);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    public User phone2User(String phoneNumber){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phoneNumber", phoneNumber);
        return userMapper.selectOne(wrapper);
    }

    public User acount2userId(String acount,String password){
        QueryWrapper<User> queryWrapper=new QueryWrapper();
        queryWrapper.eq("account",acount);
        queryWrapper.eq("password",password);
        User user=userMapper.selectOne(queryWrapper);
        if(user!=null)
            return user;
        return null;
    }

    //判断是否有这个账号密码
    public boolean cheak(String account,String passwork){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("password",passwork);
        List<User> users=userMapper.selectList(queryWrapper);
        if (users.size()>0){
            //成功，存在
            return true;
        }else {
            return false;
        }
    }
    public User getUserByAccount(String account){
        return userMapper.selectOne(new QueryWrapper<User>().eq("account",account));
    }
}

